Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.Configuration
Imports System.Data
Imports System.Data.SqlClient

Public Partial Class TheatreStaff_InsertSessionDates
	Inherits System.Web.UI.Page
	Protected Sub Page_Load(sender As Object, e As EventArgs)
		'srcgrdSession.SelectParameters["username"].DefaultValue = Page.User.Identity.Name;
		'srcgrdSchedule.SelectParameters["username"].DefaultValue = Page.User.Identity.Name;
		'grdSpecialTickets.Sort("MovieTitle", SortDirection.Ascending);
		Dim connectionString As String
		connectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

		Dim con As New SqlConnection(connectionString)
		Dim cmd As New SqlCommand("SELECT TheatreId from TheatreStaff WHERE TheatreStaffUsername=@username", con)

		cmd.Parameters.AddWithValue("@username", Page.User.Identity.Name)

		Using con
			con.Open()
			Dim TheatreID As Object = cmd.ExecuteScalar()
			If TheatreID IsNot Nothing Then
				lblId.Text = TheatreID.ToString()
			End If
		End Using
		Dim con1 As New SqlConnection(connectionString)
		Dim cmd1 As New SqlCommand("SELECT Theatrename from Theatre WHERE Theatreid=@theatreid", con1)

		cmd1.Parameters.AddWithValue("@theatreid", lblId.Text)


		Using con1
			con1.Open()
			Dim Theatrename As Object = cmd1.ExecuteScalar()

			If Theatrename IsNot Nothing Then

				Label51.Text = Theatrename.ToString()
			End If
		End Using

    End Sub
    Protected Sub Clear(ByVal sender As Object, ByVal e As EventArgs)
        Label15.Text = String.Empty
        Label16.Text = String.Empty
    End Sub
    Protected Sub ClearAll(ByVal sender As Object, ByVal e As EventArgs)
        Label15.Text = ""
        Label16.Text = ""
    End Sub
    Protected Sub Cancel(ByVal sender As Object, ByVal e As EventArgs)

        txtDateI.Text = String.Empty
        ddlMovieTitleI.SelectedIndex = -1
        ddlscheduleI.SelectedIndex = -1
        'ddlSessionI.SelectedItem.Text = "Select";
        ddlTypeI.SelectedIndex = -1
        ddlCinema.SelectedIndex = -1
        Label15.Text = ""
        Label16.Text = ""
        'grdSession.DataBind();
    End Sub

    Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
        Label15.Text = ""
        Label16.Text = ""
        Dim connectionString As String
        connectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim con As New SqlConnection(connectionString)
        Dim con1 As New SqlConnection(connectionString)
        Dim cmd1 As New SqlCommand("INSERT datesession(date,sessionid,movieid,sessiontype,scheduleid) values (@date,@sessionid,@movieid,@sessiontype,@scheduleid)", con1)
        Dim cmd As New SqlCommand("SELECT datesession.date,datesession.sessionid from datesession,Movie,TheatreStaff WHERE TheatreStaff.TheatreId=Movie.TheatreId AND datesession.movieid=Movie.MovieId AND Movie.TheatreId=@TheatreId AND datesession.date=@date AND datesession.sessionid=@sessionid", con)
        Dim ParamString As String = lblId.Text
        Dim RowCount As Int32
        Dim HasMovies As [Boolean]

        cmd.Parameters.AddWithValue("@TheatreId", ParamString)
        cmd.Parameters.AddWithValue("@date", DateTime.Parse(txtDateI.Text))
        cmd.Parameters.AddWithValue("@sessionid", ddlSessionI.SelectedValue)
        'cmd.Parameters.AddWithValue("@movieid", ddlMovieTitleI.SelectedValue );

        Using con
            con.Open()
            Dim Reader As SqlDataReader = cmd.ExecuteReader()

            If Reader.HasRows Then

                Label15.Text = "The session date " + txtDateI.Text + " already exist in the database!"

                'lblCheck.Text = string.Empty;
                'ClientScript.RegisterClientScriptBlock(ClientScript.GetType(), "Focus", "<script language='JavaScript' 'showInsert();'/>");


                HasMovies = True
            Else

                HasMovies = False
            End If
        End Using
        Using con1
            con1.Open()
            If HasMovies = False Then
                cmd1.Parameters.AddWithValue("@date", DateTime.Parse(txtDateI.Text))
                cmd1.Parameters.AddWithValue("@sessionid", ddlSessionI.SelectedValue)
                cmd1.Parameters.AddWithValue("@movieid", ddlMovieTitleI.SelectedValue)
                cmd1.Parameters.AddWithValue("@sessiontype", ddlTypeI.SelectedValue)
                cmd1.Parameters.AddWithValue("@scheduleid", ddlscheduleI.SelectedValue)
                RowCount = cmd1.ExecuteNonQuery()
                txtDateI.Text = String.Empty
                ddlMovieTitleI.SelectedIndex = -1

                'ddlSessionI.SelectedItem.Text = "Select";
                ddlTypeI.SelectedIndex = -1
                ddlCinema.SelectedIndex = -1
                'if (RowCount > 0)
                '{
                'ddlscheduleI.
                If ddlscheduleI.SelectedValue <> "-1" Then
                    Label16.Text = "The session " + txtDateI.Text + " has been added sucessfully!"
                End If

                '}
                ddlscheduleI.SelectedIndex = -1

            End If
        End Using
        'grdSession.DataBind();
    End Sub
    Private Sub Page_Load()
        'if (!Page.IsPostBack)
        'grdSession.SelectedIndex = 0;
        'Label15.Text = string.Empty;
        'Label16.Text = string.Empty;
    End Sub
    Protected Sub OnItemInserted_Click(ByVal sender As Object, ByVal e As DetailsViewInsertedEventArgs)
        'grdSession.DataBind();
        Label15.Text = ""
        Label16.Text = ""

    End Sub
    Protected Sub OnItemUpdated_Click(ByVal sender As Object, ByVal e As DetailsViewUpdatedEventArgs)
        'grdSession.DataBind();
        Label15.Text = ""
        Label16.Text = ""
    End Sub
End Class
